package com.atguigu.gmall.product.service.impl;

import brave.internal.collect.Lists;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseAttrValue;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import com.atguigu.gmall.product.mapper.BaseAttrValueMapper;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BaseAttrValueServiceImpl extends ServiceImpl<BaseAttrValueMapper, BaseAttrValue> implements BaseAttrValueService {

    @Autowired
    BaseAttrValueMapper baseAttrValueMapper;

    @Autowired
    BaseAttrInfoMapper baseAttrInfoMapper;

    @Override
    public List<BaseAttrValue> getAttrValueList(Long attrId) {

        QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id",attrId);

        List<BaseAttrValue> list = baseAttrValueMapper.selectList(wrapper);
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdateAttrInfoAndValue(BaseAttrInfo baseAttrInfo) {
        log.info("前端提交的数据：{}",baseAttrInfo);

        //新增
        if (baseAttrInfo.getId() == null){
            saveInfoAndValue(baseAttrInfo);
        }else{
            //更新
            updateInfoAndValue(baseAttrInfo);

        }

    }

    /**
     * 更新平台属性值
     * @param baseAttrInfo
     */
    private void updateInfoAndValue(BaseAttrInfo baseAttrInfo) {
        //属性名更新
        baseAttrInfoMapper.updateById(baseAttrInfo);
        //属性名id
        Long id = baseAttrInfo.getId();

        QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id",id);
        baseAttrValueMapper.delete(wrapper);
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        for (BaseAttrValue baseAttrValue : attrValueList) {
            baseAttrValue.setAttrId(id);
            baseAttrValueMapper.insert(baseAttrValue);
        }
    }


    /**
     * 保存平台属性
     * @param baseAttrInfo
     */
    private void saveInfoAndValue(BaseAttrInfo baseAttrInfo) {
        //1、保存属性名
        baseAttrInfoMapper.insert(baseAttrInfo);
        //获取到刚才保留的记录的id
        Long id = baseAttrInfo.getId();

        //2、保存属性值
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        if (!CollectionUtils.isEmpty(attrValueList)){
            for (BaseAttrValue baseAttrValue : attrValueList) {
                baseAttrValue.setAttrId(id);
                baseAttrValueMapper.insert(baseAttrValue);
            }
        }
    }
}
